﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using studentskicentarETFa;
using System.Data.SqlClient;
using System.Data;
using MySql.Data.MySqlClient;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for WindowRecepcioner.xaml
    /// </summary>
    public partial class WindowRecepcioner : Window
    {
        public WindowRecepcioner()
        {
            InitializeComponent();
        }

        //dodavanje studenta
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            DodavanjeStudenta ds = new DodavanjeStudenta(PregledClanovaGrid);
            ds.Show();
            
        }


        //za brisanje i refresh studenta
        private void BindMyDataStudent()
        {
            MySqlConnection con = null;
            string connectionString = "server=localhost;user=root;pwd=;database=centar";
            con = new MySqlConnection(connectionString);
            try
            {
                con.Open();
                MySqlCommand comm = new MySqlCommand("SELECT * FROM Student", con);
                comm.CommandText = "SELECT * from Student";
                DataSet ds = new DataSet();
                MySqlDataAdapter da = new MySqlDataAdapter(comm);
                da.Fill(ds);
                PregledClanovaGrid.ItemsSource = ds.Tables[0].DefaultView;
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message.ToString());
            }
            finally
            {
                con.Close();
            }
        }

        //brisanje studenta
        private void ObrisiStudButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {

                string MyConnection4 = "server=localhost;user=root;pwd=;database=Centar";
                string Query = "delete from Student where ID_Clana='" + this.TextBoxStudent.Text + "';";
                MySqlConnection MyConn4 = new MySqlConnection(MyConnection4);
                MySqlCommand MyCommand4 = new MySqlCommand(Query, MyConn4);
                MySqlDataReader MyReader4;
                MyConn4.Open();
                MyReader4 = MyCommand4.ExecuteReader();
                MessageBox.Show("Uspjesno ste obrisali studenta.");
                while (MyReader4.Read())
                MyConn4.Close();
                BindMyDataStudent();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);

            }
        }

        //za ispis u dataGrid
        private void PregledClanovaGrid_Loaded(object sender, RoutedEventArgs e)
        {
            MySqlConnection con = null;
            studentskicentarETFa.DAL d = studentskicentarETFa.DAL.Instanca;
            string connectionString = "server=localhost;user=root;pwd=;database=centar";
            con = new MySqlConnection(connectionString);

            try
            {

                con.Open();
                MySqlCommand comm = new MySqlCommand("SELECT * FROM Student", con);
                comm.CommandText = "SELECT * from Student";
                DataSet ds = new DataSet();
                MySqlDataAdapter da = new MySqlDataAdapter(comm);
                da.Fill(ds);
                PregledClanovaGrid.ItemsSource = ds.Tables[0].DefaultView;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            finally
            {
                con.Close();
            }
        }

       

        private void PrintanjeButton_Click(object sender, RoutedEventArgs e)
        {
            PrintDialog printDialog = new PrintDialog();
            if (printDialog.ShowDialog() == true)
            {
                printDialog.PrintVisual(grid, "My First Print Job");
            }
        }

       

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            MainWindow m = new MainWindow();
            m.Show();
            this.Hide();
        }

        private void Grid_ContextMenuOpening(object sender, ContextMenuEventArgs e)
        {
         
            ContextMenuStrip1.Items.Clear();
            ContextMenuStrip1.Items.Add("Obrisi");
        }

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            webBrowser2.Navigate("https://mail.google.com");
        }
        

      
     

    }
}
